<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div>
                <header style="height: 100%">
					 <div align="left">
                        <table style="width: 100%">
                            <tr>
                                <td>
                                	<form class="form-inline" onsubmit="return false">
										<div class="form-group">
											文件名：
											<input id="name" type="text" class="form-control" placeholder="文件名">
											时间：
											<input type="text" class="form-control" placeholder="开始时间" id="beginTime">
											<input type="text" class="form-control" placeholder="结束时间" id="endTime">
										</div>
                                        <button id="searchBt" class="layui-btn layui-btn-sm" permission="file:query"><i class="layui-icon">&#xe615;</i>搜索</button>
									</form>
                                </td>
                                <td align="right">
                                	<select class="input-sm" id="fileSource">
                                		<option value="LOCAL">本地上传</option>
                                		<option value="ALIYUN">阿里云上传</option>
                                	</select>
                                    <button type="button" class="layui-btn" id="test1">
                                        <i class="layui-icon">&#xe67c;</i>上传文件
                                    </button>
                                </td>
                            </tr> 
                        </table>
                    </div>
                </header>
                
                <div>
                    <div class="widget-body no-padding">
                        <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                            <thead>
                                <tr>
                                </tr>                       
                                <tr>
                                    <th>文件名</th>
                                    <th>url</th>
                                    <th>存储位置</th>
                                    <th>path</th>
                                    <th>contentType</th>
                                    <th>上传时间</th>
                                    <th>管理</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>


<script src="../../js/constant.js"></script>
<script type="text/javascript" src="../../js/libs/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript">
var pers = checkPermission();

var example;

layui.use(['layer','util', 'laydate'], function(){
    var layer = layui.layer;
    var util = layui.util;
    var laydate = layui.laydate;
    laydate.render({
       elem: '#beginTime'
    });
    laydate.render({
       elem: '#endTime'
    });
    
    function init(){
    	example = 
        	$('#dt-table').DataTable({
            	"searching": false,
            	"processing": false,
            	"serverSide" : true,
            	"ordering" :false,
            	"language": {
                    "url": "../../js/plugin/datatables/Chinese.lang"
                },
            	"ajax": {
            		"url" : domainName + "/api-f/files",
            		"type":"get",
            		"data":function(d){
        				d.name = $('#name').val();
        				d.beginTime = $('#beginTime').val();
        				d.endTime = $('#endTime').val();
            		},
            		"dataSrc": function (json) {
            			json.recordsTotal=json.total;
            			json.recordsFiltered=json.total;
            			return json.data;
            		},
        			"error":function(xhr, textStatus, errorThrown){
        				var msg = xhr.responseText;
        				console.log(msg);
        				if (xhr.status == 400) {
        					layer.msg(JSON.parse(msg).message);
        				} else if (xhr.status == 401) {
        					localStorage.removeItem("token");
        					layer.msg("token过期，请重新登录", {shift: -1, time: 1000}, function(){
                                location.href = loginPage;
                            });
        				} else if (xhr.status == 403) {
        					layer.msg('未授权');
        				} else if (xhr.status == 500) {
        					var info = JSON.parse(msg);
        					var exception = info.exception;
        					var message = info.message;
        					layer.msg('系统错误：' + (exception ? exception : message));
        				}
        			}
            	},
            	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
                "columns": [
                	{ "data": "name", "defaultContent": ""},
                	{ 
                    	"data": "url", 
                    	"defaultContent": "",
                    	"render": function (data, type, row) {
                    		var url = row['url'];
                    		var isImg = row['isImg'];
                    		var u = "";
                    		if(isImg) {
                    			return "<a target='_blank' href='" + url +"'><img width='30' src='" + url + "'></img></a>";
                    		} else {
                    			return url;
                    		}
                      	}
                    },
                	{ 
                    	"data": "source", 
                    	"defaultContent": "",
                    	"render": function (data, type, row) {
                    		if(data =="LOCAL"){
                    			return "本地";
                    		} else if(data == "ALIYUN") {
                    			return "阿里云";
                    		}
                    		return "";
                      	}
                    },
                    { "data": "path", "defaultContent": ""},
                    { "data": "contentType", "defaultContent": ""},
                    { 
                    	"data": "createTime",
                    	"defaultContent": "",
                    	"render": function (data, type, row) {
                    		return util.toDateString(data);
                    	}
                    },
                    { 
                    	"data": "", 
                    	"defaultContent": "",
                    	"orderable":false,
                    	"render": function (data, type, row) {
                    		var id = row['id'];
                            var del = buttonDel(id, "file:del", pers);
                            return del;
                      	}
                    },
                ],
            } );
    }

    init();
});

function del(id){
	layer.confirm('确定要删除吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
    	$.ajax({
            type : 'delete',
            url : domainName + '/api-f/files/'+id,
            success : function(data) {
                example.ajax.reload();
                layer.msg("删除成功");
            }
        });
        
        layer.close(1);
    });
}

$("#searchBt").click(function(){
	example.ajax.reload();
});

layui.use('upload', function(){
	var upload = layui.upload;
	
	upload.render({
	    elem: '#test1' //绑定元素
	    ,accept: 'file' //允许上传的文件类型
	    ,url: domainName + '/zuul/api-f/files?fileSource=' + $("#fileSource").val()
	    ,done: function(res, index, upload){
	    	layer.msg("上传成功");
		 	example.ajax.reload();
	    }
	  });
	
	
	$("#fileSource").change(function(){
		var url = domainName + '/zuul/api-f/files?fileSource=' + $("#fileSource").val();
		$("#test1").attr("lay-data", "{url: '" + url + "'}");
	});
});

</script>
</html>